package com.zsy.aspect;

import com.zsy.entity.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Component("myAspect")
@Aspect
public class MyAspect {

    private final static Logger logger= LoggerFactory.getLogger(MyAspect.class);

    //@Before指在切点方法之前执行,也就是在Controller层方法执行之前执行,这里可以通过JoinPoint获取一些有关方法的信息,在这里也可以修改参数的值

    //@Before()括号里设置的是切点方法的名称
    @Before("execution(public * com.maheimei.controller..*.*(..))")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求，记录请求内容
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        User user =(User)(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
//        // 记录下请求内容
//        // url
//        logger.info("URL : " + request.getRequestURI().toString());
//        // 请求类型
//        logger.info("HTTP_METHOD : " + request.getMethod());
//        // ip地址
//        logger.info("IP : " + request.getRemoteAddr());
//        // 被请求接口的绝对路径
//        logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
//        // 被请求方法的参数
//        logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
//        // 用户名
//        logger.info("用户 : " + user.getUsername());
    }

}